from django.db import models
import datetime


JD = 0
TB = 1
SN = 2
PLATFORM_CHOICES = [
    (JD, "京东"),
    (TB, "淘宝"),
    (SN, "苏宁"),
]


class Product(models.Model):
    search_word = models.CharField(max_length=32, default="")
    platform = models.IntegerField(choices=PLATFORM_CHOICES, default=JD)
    product_id = models.CharField(max_length=32, null=False, default="Unknown")
    name = models.CharField(max_length=256, null=False, default="Unknown")
    price = models.FloatField(default=0.0)
    pic_url = models.CharField(max_length=128, default="Unknown")
    merchant = models.CharField(max_length=64, default="")
    add_date = models.DateField(default=datetime.datetime.now)

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=["search_word", "platform", "product_id"],
                                    name="unique_word_platform_id")
        ]


class TraceProduct(models.Model):
    product_name = models.CharField(max_length=256, null=False, default="Unknown")
    platform = models.IntegerField(choices=PLATFORM_CHOICES, default=JD)
    user_id = models.IntegerField(null=False, default=-1)
    product_id = models.CharField(max_length=32, null=False, default="Unknown")
    target_price = models.FloatField(null=False, default=-1)
    add_date = models.DateField(default=datetime.datetime.now)

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=["platform", "user_id", "product_id"],
                                    name="unique_user_platform_id"),
        ]


class HistoryProduct(models.Model):
    platform = models.IntegerField(choices=PLATFORM_CHOICES, default=JD)
    product_id = models.CharField(max_length=32, null=False, default="Unknown")
    price = models.FloatField(default=0.0)
    add_date = models.DateField(default=datetime.datetime.now)

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=["platform", "product_id", "add_date"],
                                    name="unique_platform_id_date"),
        ]
